#include "gp_dp1_ctl.h"
#include "sock_msg_ctl.h"
#include "gp_layout_map.h"
#include "stdio.h"
#include "string.h"
#include "../lua/action_msg.h"
#include "resource_manage.h"
#include <unistd.h>
#include "gp_logistic_ctl.h"
#include "gp_dj_ctl.h"
#include "gp_qpcr_ctl.h"

int logistic2_move_pos(int posTypeIn);


void logisticActFunc(void * paramIn,void * actResultOut)
{
    if(!paramIn || !actResultOut)
    {
        return;
    }
    printf("enter in logisticActFunc\n");
    act_msg_node * pstActMsg = (act_msg_node *)paramIn;
    int actSuccess = 0;
    
    if(pstActMsg->msgHead.actType == ACT_SINGLE_MOVE)
    {
        if(pstActMsg->msgHead.paramType == PARAM_AXIS_MOVE_STEPS)
        {
            int component = 0;
            char * pData = (char *)(pstActMsg->paramData);
            memcpy(&component,pData,sizeof(int));

            pData += sizeof(int);

            int steps = 0;
            memcpy(&steps,pData,sizeof(int));

            actSuccess = logistic_single_move_steps(&steps,component);

        }
        else if(pstActMsg->msgHead.paramType == PARAM_AXIS_MOVE_POS)
        {
            int component = 0;
            char * pData = (char *)(pstActMsg->paramData);
            memcpy(&component,pData,sizeof(int));

            pData+= sizeof(int);

            actSuccess = logistic_single_move_pos(pData,component);

        }
    }
    else if(pstActMsg->msgHead.actType == ACT_PARRAL_MOVE)
    {
        if(pstActMsg->msgHead.paramType == PARAM_AXIS_MOVE_POS)
        {
            // location_layout locLayout;
            // location_axis locAxis;
            // memcpy(&locLayout,((char*)pstActMsg->paramData) + sizeof(int),sizeof(location_layout));
            // printf("location_type:%d,mx:%d,my:%d,ix:%d,iy:%d\n",
            // locLayout.location_type,
            // locLayout.indexX,
            // locLayout.indexY,
            // locLayout.column_x,
            // locLayout.row_y);
            // CGpLayout * pstLayout = CGpLayout::get_instance();
            // int convertRet = pstLayout->convertToAxisLoc(&locLayout,&locAxis);
            // if(convertRet)
            // {
            //     printf("convert location failed,errorNum:%d\n",convertRet);
            // }
            // else 
            // {
            //     printf("x=%d,y=%d\n",locAxis.x,locAxis.y);
                
            //     actSuccess = dp1XY_move_beta(&locAxis);
            // }
            actSuccess = logistic_module_move(pstActMsg->paramData);

        }
        else if(PARAM_LOGISTIC_MOVE == pstActMsg->msgHead.paramType)
        {
            actSuccess = logistic_module_move(pstActMsg->paramData);
        }
    }
    else if(pstActMsg->msgHead.actType == ACT_SINGLE_INIT)
    {
        int component = -1;
        memcpy(&component,(char*)pstActMsg->paramData,sizeof(int));
        printf("logisticActFunc,single init,componet:%d\n",component);
        actSuccess = logistic_single_init(component);//(NULL,component);
    }
    else if(pstActMsg->msgHead.actType == ACT_MODULE_INIT)
    {
        printf("logisticActFunc,module init\n"); 
        //actSuccess = dp1_module_init(NULL);       
    } 
    else if(ACT_TRANS_BOARD == pstActMsg->msgHead.actType)
    {
        actSuccess = logistic_trans_board(pstActMsg->paramData);
    }
    else if(ACT_JAW_TRANS == pstActMsg->msgHead.actType)
    {
        location_layout locLayoutSrc;
        location_layout locLayoutDst;
        char * pData = (char *)pstActMsg->paramData;

        memcpy(&locLayoutSrc,pData,sizeof(location_layout));

        pData += sizeof(location_layout);

        memcpy(&locLayoutDst,pData,sizeof(location_layout));

        printf("loc src type:%d\n",locLayoutSrc.location_type);

        if(locLayoutSrc.location_type == DJ_QPCR_BOARDJAW)
        {
            //1,log1 goto wait pos
            actSuccess = logistic1_move_pos(LOG1_PCR_BOARD);
            if(actSuccess==1)
            {
                actSuccess = djQ_transBoard(pstActMsg->paramData);
            }
            else
            {
                printf("before dj jaw trans, log1 goto pcr board pos failed\n");
            }            

        }
        else
        {
            //3rd logistic trans board.
            actSuccess = qpcr_trans_board(pstActMsg->paramData);
        }
    }

    msgAckSock * pstResult = (msgAckSock *)actResultOut;

    if(!actSuccess)
    {
        pstResult->status = 1;
    }
    else
    {
        pstResult->status = 0;
    }
}

int convertLogisticToSid(int componentIn)
{
    int sid = -1;

    switch (componentIn)
    {
        case MT_LOGISTICS1_Y:
        sid = SID_LOG_P_Y;
        break; 
        case MT_LOGISTICS1_Z:
        sid = SID_LOG_P_Z;        
        break;
        case MT_LOGISTICS2_BASE_X:
        sid = SID_LOG_P_X;        
        break;
        case MT_LOGISTICS2_TRAY_X:
        sid = SID_LOG_P_X_CHANGE;        
        break;
        case MT_LOGISTICS3_TRAY_Z:
        sid = SID_LOG_Q_UP;        
        break;
        case MT_JAW_Z:
        sid = SID_LOG_Q_Z;
        break;
        case MT_JAW_X:
        sid = SID_LOG_Q_X;
        break;
        case MT_JAW:
        sid = SID_LOG_Q_JAW;
        break;       

        default:
        break;
    }
    return sid;
}

int logistic1_move_pos(int posTypeIn)
{
    int actSuccess = 0;
    if(posTypeIn < LOG1_PCR_BOARD || posTypeIn > LOG1_EXCHANGE12)
    {
        return actSuccess;
    }

    move_param moveParamArr[2];
    memset(moveParamArr,0,sizeof(move_param)*2);

    CGpLayout * pstLayoutHandle = CGpLayout::get_instance();
    if(pstLayoutHandle == NULL)
    {
        printf("logistic1_move_pos, get layout handle failed\n");
        return 0;        
    }

    logistic_loc locLog1;
    int calLogLocRet = pstLayoutHandle->getLogisticLoc(posTypeIn,&locLog1);

    if(calLogLocRet)
    {
        printf("logistic1_move_pos, calc the target steps failed\n");
        return 0;          
    }

    if(posTypeIn == LOG1_DOWN)
    {
        moveParamArr[0].sid = SID_LOG_P_Z;
        moveParamArr[0].pos = locLog1.z_change;
        moveParamArr[0].speed = 20000;
        moveParamArr[0].inc_speed = 80000;        
        moveParamArr[0].dec_speed = 80000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

        if(actSuccess!=1)
        {
            printf("logistic1_move_pos, z go down failed\n");
            return 0;
        }

        moveParamArr[0].sid = SID_LOG_P_Y;
        moveParamArr[0].pos = locLog1.x_y_base;
        moveParamArr[0].speed = 8000;
        moveParamArr[0].inc_speed = 20000;        
        moveParamArr[0].dec_speed = 20000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

        return actSuccess;
    }

    // moveParamArr[0].sid = SID_LOG_P_Z;
    // moveParamArr[0].pos = 0;
    // moveParamArr[0].speed = 4000;
    // moveParamArr[0].inc_speed = 8000;        
    // moveParamArr[0].dec_speed = 8000; 


    // actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

    // if(actSuccess!=1)
    // {
    //     printf("logistic1_move_pos, z go down failed\n");
    //     return 0;
    // }

    moveParamArr[0].sid = SID_LOG_P_Y;
    moveParamArr[0].pos = locLog1.x_y_base;
    moveParamArr[0].speed = 8000;
    moveParamArr[0].inc_speed = 20000;        
    moveParamArr[0].dec_speed = 20000; 


    actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

    if(actSuccess!=1)
    {
        printf("logistic1_move_pos, y go to target failed\n");
        return 0;
    }


    moveParamArr[0].sid = SID_LOG_P_Z;
    moveParamArr[0].pos = locLog1.z_change;
    moveParamArr[0].speed = 20000;
    moveParamArr[0].inc_speed = 80000;        
    moveParamArr[0].dec_speed = 80000; 


    actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

    if(actSuccess!=1)
    {
        printf("logistic1_move_pos, z go to target failed\n");
        return 0;
    }

    return actSuccess;        
}

int logistic1_trans(int posTypeIn)
{
    int actSuccess = 0;
    if(posTypeIn < LOG1_PCR_BOARD || posTypeIn > LOG1_EXCHANGE12)
    {
        return actSuccess;
    }

    move_param moveParamArr[2];
    memset(moveParamArr,0,sizeof(move_param)*2);

    CGpLayout * pstLayoutHandle = CGpLayout::get_instance();
    if(pstLayoutHandle == NULL)
    {
        printf("logistic1_trans, get layout handle failed\n");
        return 0;        
    }

    logistic_loc locLog1;
    int calLogLocRet = pstLayoutHandle->getLogisticLoc(posTypeIn,&locLog1);

    if(calLogLocRet)
    {
        printf("logistic1_trans, calc the target steps failed\n");
        return 0;          
    }



    if(posTypeIn == LOG1_DOWN || posTypeIn == LOG1_EXCHANGE12)
    {
        actSuccess = logistic2_move_pos(LOG2_EXCHANGE12);

        if(actSuccess!=1)
        {
            printf("logistic1_trans, logistic2_move_pos failed\n");
            return 0;
        }

        actSuccess = logistic1_move_pos(LOG1_EXCHANGE12);

        if(actSuccess!=1)
        {
            printf("logistic1_trans, logistic1_move_pos 1 failed\n");
            return 0;
        }

        actSuccess = logistic1_move_pos(LOG1_DOWN);

        if(actSuccess!=1)
        {
            printf("logistic1_trans, logistic1_move_pos 2 failed\n");
            return 0;
        }
    }

    return actSuccess;        
}

int logistic3_move_pos(int posTypeIn)
{
    int actSuccess = 0;
    if(posTypeIn < LOG2_EXCHANGE23 || posTypeIn > LOG3_BATH2)
    {
        return actSuccess;
    }

    move_param moveParamArr[2];
    memset(moveParamArr,0,sizeof(move_param)*2);

    CGpLayout * pstLayoutHandle = CGpLayout::get_instance();
    if(pstLayoutHandle == NULL)
    {
        printf("logistic3_move_pos, get layout handle failed\n");
        return 0;        
    }

    logistic_loc locLog3;
    int posTypeActual = posTypeIn;

    if(posTypeActual == LOG2_EXCHANGE23)
    {
        posTypeActual = LOG3_UP;
    }
    int calLogLocRet = pstLayoutHandle->getLogisticLoc(posTypeActual,&locLog3);

    if(calLogLocRet)
    {
        printf("logistic3_move_pos, calc the target steps failed\n");
        return 0;          
    }
    printf("in logistic3_move_pos, postype:%d\n",posTypeIn);

    if(posTypeIn == LOG3_UP)
    {
        moveParamArr[0].sid = SID_LOG_Q_UP;
        moveParamArr[0].pos = locLog3.z_change;
        moveParamArr[0].speed = 20000;
        moveParamArr[0].inc_speed = 80000;        
        moveParamArr[0].dec_speed = 80000; 


        return uimMoveAct_multi(moveParamArr,1,20000);        
    }
    else  if(posTypeIn == LOG2_EXCHANGE23)
    {
        moveParamArr[0].sid = SID_LOG_Q_UP;
        moveParamArr[0].pos = locLog3.x_y_base;
        moveParamArr[0].speed = 20000;
        moveParamArr[0].inc_speed = 80000;        
        moveParamArr[0].dec_speed = 80000; 


        return uimMoveAct_multi(moveParamArr,1,20000);        
    }

    moveParamArr[0].sid = SID_LOG_Q_Z;
    moveParamArr[0].pos = 0;
    moveParamArr[0].speed = 20000;
    moveParamArr[0].inc_speed = 80000;        
    moveParamArr[0].dec_speed = 80000; 


    actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

    if(actSuccess!=1)
    {
        printf("logistic3_move_pos, jaw z go up failed\n");
        return 0;
    }

    moveParamArr[0].sid = SID_LOG_Q_X;
    moveParamArr[0].pos = locLog3.x_y_base;
    moveParamArr[0].speed = 20000;
    moveParamArr[0].inc_speed = 80000;        
    moveParamArr[0].dec_speed = 80000; 


    actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

    if(actSuccess!=1)
    {
        printf("logistic3_move_pos, x go to target failed\n");
        return 0;
    }


    moveParamArr[0].sid = SID_LOG_Q_Z;
    moveParamArr[0].pos = locLog3.z_change;
    moveParamArr[0].speed = 20000;
    moveParamArr[0].inc_speed = 80000;        
    moveParamArr[0].dec_speed = 80000; 


    actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

    if(actSuccess!=1)
    {
        printf("logistic3_move_pos, z go to target failed\n");
        return 0;
    }

    return actSuccess;        
}

int logistic2_move_pos(int posTypeIn)
{
    int actSuccess = 0;
    if(posTypeIn < LOG2_EXCHANGE12 || posTypeIn > LOG2_EXCHANGE23)
    {
        return actSuccess;
    }

    CGpLayout * pstLayoutHandle = CGpLayout::get_instance();
    if(pstLayoutHandle == NULL)
    {
        printf("logistic2_move_pos, get layout handle failed\n");
        return 0;        
    }

    logistic_loc locLog1;
    int calLogLocRet = pstLayoutHandle->getLogisticLoc(posTypeIn,&locLog1);

    if(calLogLocRet)
    {
        printf("logistic2_move_pos, calc the target steps failed\n");
        return 0;          
    }

    move_param moveParamArr[2];
    memset(moveParamArr,0,sizeof(move_param)*2);

    if(get_x_change_curr_pos() > X_CHANGE_SAFE_POS 
    && locLog1.z_change < X_CHANGE_SAFE_POS)
    {
        moveParamArr[0].sid = SID_LOG_P_X_CHANGE;
        moveParamArr[0].pos = X_CHANGE_SAFE_POS;
        moveParamArr[0].speed = 30000;
        moveParamArr[0].inc_speed = 120000;        
        moveParamArr[0].dec_speed = 120000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,40000);

        if(actSuccess!=1)
        {
            printf("logistic2_move_pos, x change go to safe pos failed\n");
            return 0;
        }         
    }

    moveParamArr[0].sid = SID_LOG_P_X;
    moveParamArr[0].pos = locLog1.x_y_base;
    moveParamArr[0].speed = 20000;
    moveParamArr[0].inc_speed = 80000;        
    moveParamArr[0].dec_speed = 80000; 


    moveParamArr[1].sid = SID_LOG_P_X_CHANGE;
    moveParamArr[1].pos = locLog1.z_change;
    moveParamArr[1].speed = 30000;
    moveParamArr[1].inc_speed = 120000;        
    moveParamArr[1].dec_speed = 120000; 


    actSuccess = uimMoveAct_multi(moveParamArr,2,40000);

    if(actSuccess!=1)
    {
        printf("logistic2_move_pos, go to target failed\n");
        return 0;
    }

    set_x_change_pos(locLog1.z_change);


    return actSuccess;        
}

int logistic_module_move(void * paramIn)
{
    char * pMsgData = (char *)paramIn;

    int ret = 0;
    if(pMsgData == NULL)
    {
        return ret;
    }
    location_layout stLocLayout;

    memcpy(&stLocLayout,pMsgData,sizeof(location_layout));

    int submodule = stLocLayout.indexX;
    int postype = stLocLayout.location_type;

    printf("logistic_module_move, module:%d,type:%d\n",submodule,postype);
    switch(submodule)
    {
        case SUB_MODULE_LOG1:
        {
            ret = logistic1_move_pos(postype);
        }
        break;
        case SUB_MODULE_LOG2:
        {
            ret = logistic2_move_pos(postype);
        }
        break;
        case SUB_MODULE_LOG3:
        {
            ret = logistic3_move_pos(postype);
        }
        break;
        default:
        break;        
    }
    return ret;
}

int logistic_trans_board(void * paramIn)
{
    char * pMsgData = (char *)paramIn;

    int ret = 0;
    if(pMsgData == NULL)
    {
        return ret;
    }
    location_layout stSrcLocLayout;
    location_layout stDstLocLayout;

    memcpy(&stSrcLocLayout,pMsgData,sizeof(location_layout));

    pMsgData += sizeof(location_layout);

    memcpy(&stDstLocLayout,pMsgData,sizeof(location_layout));    

    int submoduleSrc = stSrcLocLayout.indexX;
    int postypeSrc = stSrcLocLayout.location_type;

    int submoduleDst = stDstLocLayout.indexX;
    int postypeDst = stDstLocLayout.location_type;

    printf("src module: %d, src type:%d, dst module:%d,dst type:%d\n",
    submoduleSrc,postypeSrc,
    submoduleDst,postypeDst);


    if(submoduleSrc==submoduleDst)
    {
        switch(submoduleSrc)
        {
            case SUB_MODULE_LOG1:
            {
                ret = logistic1_trans(postypeDst);
            }
            break;
            case SUB_MODULE_LOG2:
            {
                ret = logistic2_move_pos(postypeDst);
            }
            break;
            case SUB_MODULE_LOG3:
            {
                ret = logistic3_move_pos(postypeDst);
            }
            break;
            default:
            break; 
        }
       
    }

    if(submoduleSrc == SUB_MODULE_LOG1 && submoduleDst == SUB_MODULE_LOG2)
    {
        ret = logistic1_trans(LOG1_DOWN);
        if(ret != 1)
        {
            printf("logistic_trans_board,logistic1_trans failed");
            return 0;
        }

        ret = logistic2_move_pos(postypeDst);
    }
    return ret;
}


int logistic_single_move_steps(void * paramIn,int component)
{
    int * pSteps = (int *)paramIn;

    int actSuccess = 0;
    int sid = convertLogisticToSid(component);

    if(sid<0)
    {
        return actSuccess;
    }

    move_param moveParam;
    memset(&moveParam,0,sizeof(move_param));

    moveParam.sid = sid;
    moveParam.pos = *pSteps;
    moveParam.speed = 10000;
    moveParam.inc_speed = 40000;
    moveParam.dec_speed = 40000;

    actSuccess = uimMoveAct_multi(&moveParam,1,20000);

    if(sid == SID_LOG_P_X_CHANGE)
    {
        set_x_change_pos(*pSteps);
    }

    return actSuccess;
}


int logistic_single_init(int component)
{
    int actSuccess = 0;
    int sid = convertLogisticToSid(component);

    if(sid<0)
    {
        return actSuccess;
    }

    move_param moveParam;
    memset(&moveParam,0,sizeof(move_param));

    moveParam.sid = sid;
    moveParam.pos = 0;
    moveParam.speed = 10000;
    moveParam.inc_speed = 40000;
    moveParam.dec_speed = 40000;

    actSuccess = uimInitAct_multi(&moveParam,1,20000);

    if(sid == SID_LOG_P_X_CHANGE)
    {
        set_x_change_pos(0);
    }

    return actSuccess;    
}

int g_currXChangePos = 0;

void set_x_change_pos(int currPosIn)
{
   g_currXChangePos =  currPosIn;
}
int get_x_change_curr_pos(void)
{
    return g_currXChangePos;
}

int qpcr_trans_board(void * paramIn)
{
    location_layout locLayoutSrc;
    location_layout locLayoutDst;
    char * pData = (char *)paramIn;
    int actSuccess = 0;

    memcpy(&locLayoutSrc,pData,sizeof(location_layout));

    pData += sizeof(location_layout);

    memcpy(&locLayoutDst,pData,sizeof(location_layout));

    move_param moveParamArr[2];
    memset(moveParamArr,0,sizeof(move_param)*2);

    CGpLayout * layoutHandle = CGpLayout::get_instance();

    if(layoutHandle == NULL)
    {
        printf("qpcr_trans_board, get layout handle failed\n");
        return 0;
    }
    
    locs_qpcr * pstlocQpcr = NULL;

    logistic_loc  log3loc;
    int calLogLocRet = 0;
    //get board
    if(locLayoutSrc.location_type != 0xffff)
    {
        //1, check if src pos is valid
        if(locLayoutSrc.location_type == LOG2_EXCHANGE23 ||
        locLayoutSrc.location_type == LOG3_UP)
        {// tray go up

            calLogLocRet = layoutHandle->getLogisticLoc(LOG3_UP,&log3loc);

            if(calLogLocRet)
            {
                printf("qpcr_trans_board, calc the target steps 0 failed\n");
                return 0;          
            }

            moveParamArr[0].sid = SID_LOG_Q_UP;
            moveParamArr[0].pos = log3loc.z_change;
            moveParamArr[0].speed = 10000;
            moveParamArr[0].inc_speed = 20000;
            moveParamArr[0].dec_speed = 20000;

            actSuccess = uimMoveAct_multi(moveParamArr,1,30000);

            if(actSuccess!=1)
            {
                printf("qpcr_trans_board, get, tray go up failed\n");
                return 0;
            }


            locLayoutSrc.location_type = LOG3_QPCR_GET;
        }

        if(locLayoutSrc.location_type != LOG3_QPCR_GET
        && locLayoutSrc.location_type != LOG3_BATH1
        && locLayoutSrc.location_type != LOG3_BATH2)

        {
            printf("qpcr_trans_board, get pos is not valid\n");
            return actSuccess;
        }


        //2, jaw x goto target pos, z go to 0
        calLogLocRet = layoutHandle->getLogisticLoc(locLayoutSrc.location_type,&log3loc);

        if(calLogLocRet)
        {
            printf("qpcr_trans_board, calc the target steps 1 failed\n");
            return 0;          
        }

        moveParamArr[0].sid = SID_LOG_Q_Z;
        moveParamArr[0].pos = 0;
        moveParamArr[0].speed = 10000;
        moveParamArr[0].inc_speed = 20000;
        moveParamArr[0].dec_speed = 20000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,30000);

        if(actSuccess!=1)
        {
            printf("qpcr_trans_board, get, z go to 0 failed\n");
            return 0;
        }

        moveParamArr[0].sid = SID_LOG_Q_X;
        moveParamArr[0].pos = log3loc.x_y_base;
        moveParamArr[0].speed = 10000;
        moveParamArr[0].inc_speed = 20000;
        moveParamArr[0].dec_speed = 20000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

        if(actSuccess!=1)
        {
            printf("qpcr_trans_board, x go to target pos failed\n");
            return 0;
        }

        //3, prepare bath

        if(locLayoutSrc.location_type == LOG3_QPCR_GET)
        {//push in the metal bath1
            pstlocQpcr = layoutHandle->getQpcrPos(0);
            moveParamArr[0].sid = SID_QPCR1_Y;
            moveParamArr[0].pos = pstlocQpcr->bath_x_work;
            moveParamArr[0].speed = 4000;
            moveParamArr[0].inc_speed = 8000;
            moveParamArr[0].dec_speed = 8000;

            actSuccess = uimMoveAct_multi(moveParamArr,1,20000);
            
        }
        else if(LOG3_BATH1 == locLayoutSrc.location_type 
        || LOG3_BATH2 == locLayoutSrc.location_type)
        {//pull out metal bath
            int qpcrId = (LOG3_BATH1 == locLayoutSrc.location_type)?0:1;

            actSuccess = qpcr_pull_out(qpcrId);
        }
        
        if(actSuccess!=1)
        {
            printf("qpcr_trans_board, bath prepare for getting failed\n");
            return 0;
        }

        //4, z go to target pos
        moveParamArr[0].sid = SID_LOG_Q_Z;//jaw z
        moveParamArr[0].pos = log3loc.z_change;
        moveParamArr[0].speed = 20000;
        moveParamArr[0].inc_speed = 80000;
        moveParamArr[0].dec_speed = 80000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,30000);

        if(actSuccess!=1)
        {
            printf("qpcr_trans_board, z go to target pos failed\n");
            return 0;
        }
        //5, jaw close
        moveParamArr[0].sid = SID_LOG_Q_JAW;
        moveParamArr[0].pos = POS_QPCR_JAW_CLOSE;
        moveParamArr[0].speed = 4000;
        moveParamArr[0].inc_speed = 8000;
        moveParamArr[0].dec_speed = 8000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,30000);

        if(actSuccess!=1)
        {
            printf("qpcr_trans_board, z go to target pos failed\n");
            return 0;
        }
        //6, jaw z go up
        moveParamArr[0].sid = SID_LOG_Q_Z;
        moveParamArr[0].pos = 5000;
        moveParamArr[0].speed = 20000;
        moveParamArr[0].inc_speed = 80000;
        moveParamArr[0].dec_speed = 80000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,30000);

        if(actSuccess!=1)
        {
            printf("qpcr_trans_board, z go to up 1 pos failed\n");
            return 0;
        }

        //7, jaw z go up 2
        moveParamArr[0].sid = SID_LOG_Q_Z;
        moveParamArr[0].pos = 0;
        moveParamArr[0].speed = 4000;
        moveParamArr[0].inc_speed = 8000;
        moveParamArr[0].dec_speed = 8000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,30000);

        if(actSuccess!=1)
        {
            printf("qpcr_trans_board, z go to up 2 pos failed\n");
            return 0;
        }

    }

    //put board

    if(locLayoutDst.location_type != 0xffff)
    {
        //1, jaw goto safe pos
        // int locTypeTemp = LOG3_BATH1; 
        // if(LOG3_QPCR_DROP == locLayoutDst.location_type 
        // || LOG3_BATH2 == locLayoutDst.location_type)
        // {
        //     locTypeTemp = LOG3_BATH1;

        // }
        // else if(LOG3_BATH1 == locLayoutDst.location_type)
        // {
        //     locTypeTemp = LOG3_BATH2; 
        // }
        // else
        // {
        //     printf("qpcr_trans_board, put pos is not valid\n");
        //     return actSuccess;
        // }

        // calLogLocRet = layoutHandle->getLogisticLoc(locTypeTemp,&log3loc);

        // if(calLogLocRet)
        // {
        //     printf("qpcr_trans_board, calc the target steps 2 failed\n");
        //     return 0;          
        // }

        // moveParamArr[0].sid = SID_LOG_Q_X;
        // moveParamArr[0].pos = log3loc.x_y_base;
        // moveParamArr[0].speed = 10000;
        // moveParamArr[0].inc_speed = 40000;
        // moveParamArr[0].dec_speed = 40000;

        // actSuccess = uimMoveAct_multi(moveParamArr,1,20000);


        // if(actSuccess!=1)
        // {
        //     printf("qpcr_trans_board, jaw x go to safe pos failed\n");
        //     return 0;
        // }

        //1, check if pos is valid
        if(LOG3_QPCR_DROP != locLayoutDst.location_type 
         && LOG3_BATH2 != locLayoutDst.location_type
         && LOG3_BATH1 != locLayoutDst.location_type)
         {
             printf("qpcr_trans_board, put pos is not valid\n");
             return actSuccess;             
         }


        //2, jaw x goto target pos
        
        calLogLocRet = layoutHandle->getLogisticLoc(locLayoutDst.location_type,&log3loc);

        if(calLogLocRet)
        {
            printf("qpcr_trans_board, calc the put target steps failed,loctype:%d\n",locLayoutDst.location_type);
            return 0;          
        }

        moveParamArr[0].sid = SID_LOG_Q_X;
        moveParamArr[0].pos = log3loc.x_y_base;
        moveParamArr[0].speed = 10000;
        moveParamArr[0].inc_speed = 20000;
        moveParamArr[0].dec_speed = 20000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

        if(actSuccess!=1)
        {
            printf("qpcr_trans_board, x go to put target pos failed\n");
            return 0;
        }


        //3,prepare bath pos
        if(locLayoutDst.location_type == LOG3_QPCR_DROP)
        {
            int qpcrId = 1;
            pstlocQpcr = layoutHandle->getQpcrPos(qpcrId);

            moveParamArr[0].sid = SID_QPCR2_Y;
            moveParamArr[0].pos = pstlocQpcr->bath_x_work;
            moveParamArr[0].speed = 10000;
            moveParamArr[0].inc_speed = 20000;
            moveParamArr[0].dec_speed = 20000;

            actSuccess = uimMoveAct_multi(moveParamArr,1,20000);

        }
        else if(locLayoutDst.location_type == LOG3_BATH1
        || locLayoutDst.location_type == LOG3_BATH2)
        {
            int qpcrId = (locLayoutDst.location_type == LOG3_BATH1)?0:1;

            actSuccess = qpcr_pull_out(qpcrId);
        }

        if(actSuccess!=1)
        {
            printf("qpcr_trans_board, bath prepare failed\n");
            return 0;
        }
        //4, jaw z go to target pos
        moveParamArr[0].sid = SID_LOG_Q_Z;//jaw z
        moveParamArr[0].pos = log3loc.z_change;
        moveParamArr[0].speed = 20000;
        moveParamArr[0].inc_speed = 80000;
        moveParamArr[0].dec_speed = 80000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,30000);

        if(actSuccess!=1)
        {
            printf("z go to target pos failed\n");
            return 0;
        }
        //5, jaw open
        moveParamArr[0].sid = SID_LOG_Q_JAW;
        moveParamArr[0].pos = POS_QPCR_JAW_OPEN;
        moveParamArr[0].speed = 4000;
        moveParamArr[0].inc_speed = 8000;
        moveParamArr[0].dec_speed = 8000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,30000);

        if(actSuccess!=1)
        {
            printf("jaw open failed\n");
            return 0;
        }
        //6, jaw z go up
        moveParamArr[0].sid = SID_LOG_Q_Z;
        moveParamArr[0].pos = 5000;
        moveParamArr[0].speed = 20000;
        moveParamArr[0].inc_speed = 80000;
        moveParamArr[0].dec_speed = 80000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,30000);

        if(actSuccess!=1)
        {
            printf("z go to up 1 pos failed\n");
            return 0;
        }

        //7, jaw z go up 2
        moveParamArr[0].sid = SID_LOG_Q_Z;
        moveParamArr[0].pos = 0;
        moveParamArr[0].speed = 4000;
        moveParamArr[0].inc_speed = 8000;
        moveParamArr[0].dec_speed = 8000;

        actSuccess = uimMoveAct_multi(moveParamArr,1,30000);

        if(actSuccess!=1)
        {
            printf("z go to up 2 pos failed\n");
            return 0;
        }
    }




}


int log3_jawx_move_pos(int postypeIn, int needPushBath)
{
    // if(needPushBath)
    // {
    //     if(postypeIn==LOG3_QPCR_GET || postypeIn==LOG3_BATH1)
    //     {

    //     }
    // }

    return 1;


}

int logistic_single_move_pos(void * paramIn,int component)
{
    int actSuccess = 0;
    location_layout locLayout;
    memcpy(&locLayout,paramIn,sizeof(location_layout));

    if(component == MT_LOGISTICS3_TRAY_Z)
    {
        actSuccess = logistic3_move_pos(locLayout.location_type);
    }

    return actSuccess;
}